/**
 * 询问框
 * @param text
 * @param href 链接地址或函数
 * @param title
 * @param okBtnText 确认按钮文本，选填
 * @param noBtnText 取消按钮文本，选填
 * @returns {Boolean}
 */
function dialogConfirm(text, href, title, okBtnText, noBtnText){
	title = CommUtil.setDefValue(title, "提示");
	JConfirmUtil.confirm(title, text, href, okBtnText, noBtnText);
}

/**
 * 提示框 异步删除
 * @param url
 * @param text 提示内容
 * @returns {boolean}
 */
function dialogConfirmAjaxDel(url, text){
	dialogConfirmAjax(url, text, '确定删除记录？');
	return false;
}

function dialogConfirmAjax(url, text, tip){
	if(CommUtil.isEmpty(url)){
		dialogTip("请求失败，地址有误", "error");
		return;
	}
	dialogConfirm(text, function(){
		HttpUtil.ajaxAsyncJsonPost(url, {}, function(data){
			dialogTip(data);
			refreshPage(1500);
		});
	}, tip);
}


/**
 * 弹出框
 * @param message
 * @param type 类型：info/success/error/warning
 */
function dialogAlert(message, type){
	title = CommUtil.setDefValue(message, "提示");
	if(type != null){
		message = JConfirmUtil.wrapTipText(type, message);
	}
	JConfirmUtil.alert(message);
}

/**
 * 提示框
 * @param msg
 * @param type info、success、error、warning
 * @param timeout 自动关闭时间（毫秒）
 * @param position top、bottom
 */
function dialogTip(msg, type, timeout, position){
	var icon = getDialogTypeIcon(type);
	NotyUtil.tip(icon+msg, type, position, timeout);
}

/**
 * 错误 提示框
 * @param msg
 * @param timeout 自动关闭时间（毫秒）
 */
function dialogTipError(msg, timeout){
	dialogTopCenterTip(msg, "error", timeout);
}

function dialogTipSuccess(msg, timeout){
	dialogTopCenterTip(msg, "success", timeout);
}

function dialogTipWarn(msg, timeout){
	dialogTopCenterTip(msg, "warning", timeout);
}


function dialogTopCenterTip(msg, type, timeout){
	dialogTip(msg, type, timeout, 'topCenter');
}

function dialogTopLeftTip(msg, type, timeout){
	dialogTip(msg, type, timeout, 'topLeft');
}

function dialogTopRightTip(msg, type, timeout){
	dialogTip(msg, type, timeout, 'topRight');
}

function dialogTopRightTipSuccess(msg, timeout){
	dialogTip(msg, "success", timeout, 'topRight');
}

function dialogBottomRightTip(msg, type, timeout){
	dialogTip(msg, type, timeout, 'bottomRight');
}


function getDialogTypeIcon(type){
	var icon = "";
	switch (type){
		case 'info':
			icon = "<i class='fa fa-info-circle text-info'></i>";
			break;
		case 'success':
			icon = "<i class='fa fa-check-circle text-success'></i>";
			break;
		case 'error':
			icon = "<i class='fa fa-times-circle text-danger'></i>";
			break;
		case 'warning':
			icon = "<i class='fa fa-exclamation-triangle text-warning'></i>";
			break;
	}
	return icon;
}


/**
 * prompt层
 * @param title
 * @param placeholder
 * @param callBack 回调函数
 * @param inputType 输入类型 text, email, tel, password, textarea 默认 text
 */
function dialogPrompt(title, placeholder, callBack, inputType){
	inputType = CommUtil.setDefValue(inputType, "text");
	JConfirmUtil.prompt(title, placeholder, callBack, inputType);
}

var __loadingId = 99999999999;

/**
 * 加载提示框
 * @param text 可为空
 * @param timeout
 */
function dialogLoading(text, timeout){
	timeout = timeout==undefined?2000:timeout;
	text = CommUtil.setDefValue(text, "加载中，请稍等...");
	text = "<span class='loading-img'>&nbsp;</span>"+text+"";
	var id = NotyUtil.tip(text, "info", "topCenter", timeout);
	__loadingId = id;
	setTimeout(function(){
		NotyUtil.closeTip(id);
		__loadingId = null;
	}, timeout);
}

function dialogLoadingClose(){
	if(__loadingId != null)
		NotyUtil.closeTip(id);
}




/**
 * 弹出 新页面
 * @param title
 * @param url
 * @param width
 * @param height
 */
function dialogOpenPage(title, url, width, height){
	JConfirmUtil.viewPage(title, url, width, height);
}


/**
 * 弹出 新页面
 * @param title
 * @param url
 * @param width
 * @param height
 */
function dialogOpenPageView(title, url, width, height){
	JConfirmUtil.viewPage(title, url, width, height);
}

/**
 * 弹出 新页面
 * @param title
 * @param htmlContent
 * @param width
 * @param height
 */
function dialogOpenPageViewHtml(title, htmlContent, width, height){
	JConfirmUtil.viewHtmlContent(title, htmlContent, width, height);
}

function dialogOpenPageConfirm(title, formUrl, width, height, okCall, initCall){
	return JConfirmUtil.confirmForm(title, formUrl, okCall, width, height, initCall);
}


function dialogOpenHtmlConfirm(title, formHtml, width, height, okCall, initCall){
	JConfirmUtil.confirmFormHtml(title, formHtml, okCall, width, height, initCall);
}


var NotyUtil = {
	/**
	 * 提示
	 * @param message
	 * @param type {info, success, error, warning}
	 * @param position {top、topCenter、topLeft、topRight、bottom、bottomCenter、bottomLeft、bottomRight}
	 * @param timeout
	 */
	tip: function (message, type, position, timeout) {
		type = CommUtil.setDefValue(type, 'info');
		timeout = CommUtil.setDefValue(timeout, 3000);
		position = CommUtil.setDefValue(position, 'topCenter');
		return noty({
			theme: 'app-noty',
			text: message,
			type: this.getType(type),
			timeout: timeout,
			layout: position,
			maxVisible: 8,
			closeWith: ['button', 'click'],
			callback: { // 回调函数
				onShow: function() {
				}, // 显示之前
				afterShow: function() {
				}, // 显示之后
				onClose: function() {
					$("#"+this.options.id).parent().remove();
				}, // 关闭之前
				afterClose: function() {
				} // 关闭之后
			},
			animation: {
				open: 'noty-animation fadeIn',
				close: 'noty-animation fadeOut'
			}
		});
	},
	closeTip: function (id) {
		$.noty.close(id); //关闭一个noty提示
	},
	closeAllTip: function () {
		$.noty.closeAll(); //关闭所有提示
	},
	getType: function (type) {
		if("info" == type){
			return "information";
		}else{
			return type;
		}
	}
}

//http://craftpip.github.io/jquery-confirm/
var JConfirmUtil = {
	// type: warning，error，success，info和question
	alert: function(message, title, btnText, callback) {
		title = CommUtil.setDefValue(title, "提示");
		btnText = CommUtil.setDefValue(btnText, "确定");
		$.alert({
			title: title,
			content: message,
			closeIcon: true,
			//icon: 'fa fa-rocket',
			buttons: {
				okay: {
					text: btnText,
					btnClass: 'btn-blue',
					action: function(){
						if(typeof callback == 'function'){
							return callback();
						}else if(typeof callback == 'string'){
							window.location.href = callback;
						}else{
							//
						}
					}
				}
			}
		});
	},
	confirm: function(title, text, callback, okBtnText, noBtnText) {
		title = CommUtil.setDefValue(title, "提示");
		okBtnText = CommUtil.setDefValue(okBtnText, "确定");
		noBtnText = CommUtil.setDefValue(noBtnText, "取消");
		$.confirm({
			title: title,
			content: text,
			closeIcon: true,
			animation: 'scale',
			closeAnimation: 'scaleX',
			buttons: {
				confirm: {
					text: okBtnText,
					btnClass: 'btn-blue',
					action: function(){
						if(typeof callback == 'function'){
							return callback();
						}else if(typeof callback == 'string'){
							window.location.href = callback;
						}else{
							dialogTipError("脚本出错，callback参数不能为空");
						}
					}
				},
				cancel: {
					text: noBtnText,
					action: function(){
						//do something
					}
				},
			}
		});

	},
	wrapTipText: function(type, content) {
		if(CommUtil.isEmpty(content))
			return "";
		type = CommUtil.setDefValue(type, "success");
		log(type+"--"+title);
		var color = "black";
		if("success" == type){
			color = "black";
		}else if("error" == type){
			color = "red";
		}else if("warning" == type){
			color = "orange";
		}
		return "<font color='"+color+"'>"+content+"</font>";
	},
	prompt: function(title, placeholder, callback, inputType) {
		//email, tel, password, textarea 默认 text
		$.confirm({
			title: title,
			content: '' +
			'<form action="" class="formName">' +
			'<div class="form-group">' +
			'<input type="'+inputType+'" placeholder="' + placeholder + '" class="name form-control" required />' +
			'</div>' +
			'</form>',
			closeIcon: true,
			buttons: {
				formSubmit: {
					text: '确定',
					btnClass: 'btn-blue',
					action: function () {
						var val = this.$content.find('.name').val();
						if (!val) {
							$.alert(placeholder + ' 不能为空');
							return false;
						}

						if (inputType === "email") {
							var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
							if (!emailRegex.test(value)) {
								$.alert(placeholder + ' 格式不正确');
								return false;
							}
						} else if (inputType === "tel") {
							var telRegex = /^[1][34578][0-9]{9}$/
							if (!telRegex.test(value)) {
								$.alert(placeholder + ' 格式不正确');
								return false;
							}
						}
						if (typeof callback == 'function') {
							return callback(val);
						} else {
							return val;
						}
					}
				},
				cancel: {
					text: '取消',
					action: function () {
						//do something
					}
				},
			},
			onContentReady: function () {
				// bind to events
				var jc = this;
				this.$content.find('form').on('submit', function (e) {
					// if the user submits the form by pressing enter in the field.
					e.preventDefault();
					jc.$$formSubmit.trigger('click'); // reference the button and click it
				});
			}
		});
	},
	confirmForm: function(title, formUrl, callback, width, height, initCall) {
		width = CommUtil.setDefValue(width, 600);
		width = CommUtil.getResponsiveWidth(width) + 'px';
		height = CommUtil.getResponsiveHeight(height) + 'px';
		return $.confirm({
			title: title,
			content: 'url:' +formUrl,
			boxWidth: width,
			boxHeight: height,
			useBootstrap: false,
			closeIcon: true,
			buttons: {
				formSubmit: {
					text: '确定',
					btnClass: 'btn-blue',
					action: function () {
						return callback();
					}
				},
				cancel: {
					text: '取消',
					action: function () {
						//do something
					}
				},
			},
			contentLoaded: function(data, status, xhr){
				if (typeof initCall == 'function') {
					initCall();
				}
			},
			onContentReady: function () {
				// bind to events
				var jc = this;
				this.$content.find('form').on('submit', function (e) {
					// if the user submits the form by pressing enter in the field.
					e.preventDefault();
					jc.$$formSubmit.trigger('click'); // reference the button and click it
				});
			}
		});
	},
	confirmFormHtml: function(title, formHtml, callback, width, height, initCall) {
		width = CommUtil.setDefValue(width, 600);
		width = CommUtil.getResponsiveWidth(width) + 'px';
		$.confirm({
			title: title,
			content: formHtml,
			boxWidth: width,
			closeIcon: true,
			useBootstrap: false,
			buttons: {
				formSubmit: {
					text: '确定',
					btnClass: 'btn-blue',
					action: function () {
						return callback();
					}
				},
				cancel: {
					text: '取消',
					action: function () {
						//do something
					}
				},
			},
			contentLoaded: function(data, status, xhr){
				if (typeof initCall == 'function') {
					initCall();
				}
			},
		});
	},
	viewPage: function(title, url, width){
		width = CommUtil.setDefValue(width, 600);
		width = CommUtil.getResponsiveWidth(width) + 'px';
		$.dialog({
			title: title,
			content: "url:"+url,
			animation: 'scale',
			closeIcon: true,
			//columnClass: 'col-md-12',
			boxWidth: width,
			useBootstrap: false,
			closeAnimation: 'scale',
			backgroundDismiss: true,
		});
	},
	viewHtmlContent: function(title, htmlContent, width){
		width = CommUtil.setDefValue(width, 600);
		width = CommUtil.getResponsiveWidth(width) + 'px';
		$.dialog({
			title: title,
			content: htmlContent,
			animation: 'scale',
			//columnClass: 'col-md-12',
			boxWidth: width,
			useBootstrap: false,
			closeIcon: true,
			closeAnimation: 'scale',
			backgroundDismiss: true,
		});
	}

}
